package com.giveu.signature.module.template.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.giveu.common.page.PageBuilder;
import com.giveu.common.page.PageInfoBT;
import com.giveu.common.page.QueryMap;
import com.giveu.signature.common.constants.HttpSysName;
import com.giveu.signature.common.service.BaseController;
import com.giveu.signature.module.template.model.SignTemplateHtml;
import com.giveu.signature.module.template.model.SignTemplateParam;
import com.giveu.signature.module.template.service.SignTemplateParamService;

@Controller
@RequestMapping("/templateParam")
public class SignTemplateParamController extends BaseController{

	private static final Logger log = LoggerFactory.getLogger(SignTemplateParamController.class);

	private static final String PREFIX = "module/template/param/";

	@Autowired
	SignTemplateParamService signTemplateParamService;

	@RequestMapping("")
	private String index(Model model) {
		model.addAttribute("httpSysNames", HttpSysName.values());
		return PREFIX + "index";
	}
	
	@RequestMapping("/edit/{id}")
	private String edit(@PathVariable("id") Long id,Model model) {
		SignTemplateParam entity = signTemplateParamService.get(id);
		model.addAttribute("entity", entity==null?new SignTemplateParam():entity);
		model.addAttribute("httpSysNames", HttpSysName.values());
		return PREFIX + "edit";
	}
	
	@RequestMapping("/modify/{id}")
	private String modify(@PathVariable("id") Long id,Model model) {
		SignTemplateParam entity = signTemplateParamService.get(id);
		if(entity!=null){
			entity.setId(null);
			entity.setModify(true);
			entity.setParamVersion(entity.getParamVersion()+1);
		}
		model.addAttribute("entity", entity==null?new SignTemplateParam():entity);
		model.addAttribute("httpSysNames", HttpSysName.values());
		return PREFIX + "edit";
	}

	@RequestMapping("/list")
	@ResponseBody
	private Object list(SignTemplateParam entity) {
	    QueryMap<SignTemplateParam> queryMap = PageBuilder.buildQueryMap();
	    queryMap.setEntity(entity);
	    PageInfo<SignTemplateParam> pageInfo = signTemplateParamService.listByPage(queryMap);
	    return new PageInfoBT<>(pageInfo);
	}
	
	@RequestMapping("/save")
	@ResponseBody
	private Object save(SignTemplateParam entity) {
		signTemplateParamService.save(entity);
	    return SUCCESS_TIP;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	private Object delete(SignTemplateParam entity) {
		signTemplateParamService.delete(entity);
		return SUCCESS_TIP;
	}
	
	@RequestMapping("/submit")
	@ResponseBody
	private Object submit(SignTemplateParam entity) {
		entity.setParamStatus(1);
		try {
			signTemplateParamService.save(entity);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return SUCCESS_TIP;
	}
	
	@RequestMapping("tryJs")
	private String tryHtml(Model model) {
		return PREFIX + "tryJs";
	}
}
